package com.gitee.wsl.concurrent.scheduler.ext

import com.gitee.wsl.concurrent.scheduler.Scheduler
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Runnable
import kotlin.coroutines.CoroutineContext

fun Scheduler.asCoroutineDispatcher(): CoroutineDispatcher = SchedulerCoroutineDispatcher(this)

internal class SchedulerCoroutineDispatcher(
    private val scheduler: Scheduler
) : CoroutineDispatcher() {

    override fun dispatch(context: CoroutineContext, block: Runnable) {
        scheduler.submit(task = block::run)
    }
}